//
//  CheckBox.m
//  OfficeBook
//
//  Created by 朱 琳 on 15/10/5.
//  Copyright (c) 2015年 sunny. All rights reserved.
//

#import "CheckBox.h"

@implementation CheckBox

-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    [self setImage:[UIImage imageNamed:@"num1"] forState:UIControlStateNormal];
    
    _selectedFlag = NO;
    [self addTarget:self action:@selector(clickCheckbox) forControlEvents:UIControlEventTouchUpInside];
    
    return self;
}

-(void)clickCheckbox {
    _selectedFlag = !_selectedFlag;
    if (_selectedFlag == YES) {
        [self setImage:[UIImage imageNamed:@"num2"] forState:UIControlStateNormal];
    }
    else {
        [self setImage:[UIImage imageNamed:@"num1"] forState:UIControlStateNormal];
    }
    if (self.delegate) {
        [self.delegate  checkBoxClick:_selectedFlag];
    }
}

-(void) setChecked {
    if (_selectedFlag == NO) {
        [self clickCheckbox];
    }
}

-(void) setUnchecked {
    if (_selectedFlag == YES) {
        [self clickCheckbox];
    }
}

@end
